 const moment = require('moment');
 const fs = require('fs');
 const path = require('path');
 const csv = require('csvtojson');

 const EtlServiceBase = require('./etlservicebase');

 class StockIndustryAnalysisService extends EtlServiceBase {
     constructor(market, sid, dataRoot) {
         super(dataRoot);
         this.append = true;
         this.dataTag = ''
         this.id = `ia.${market}`;
         this.url = 'http://f10.eastmoney.com/IndustryAnalysis/IndustryAnalysisAjax';
         this.params['code'] = `${market}${sid}`;
         this.params['icode'] = '447';
         this.fields = {};
     }
 }

 function getMarketAbbr(sid) {
     if (sid.startsWith('60')) return 'sh';
     if (sid.startsWith('000')) return 'sz';
     if (sid.startsWith('300')) return 'sz';
     return '';
 }


 function schedule(task) {
     setTimeout(task, 1000 * Math.random());
 }

 csv()
     .fromFile(path.join(__dirname, './data/eastmoney/market/1.csv'))
     .then((stocks) => {
         stocks.forEach(stock => {
             var market = getMarketAbbr(stock.code);
             if (market == '') return;
             schedule(() => {
                 console.info(`[info] request industry analysis data for ${stock.name}(${stock.code})...`);
                 new StockIndustryAnalysisService(market, stock.code, './data/eastmoney/ia').start({}, (s, r) => {
                     r.objects = r.objects.map(o => {
                         return {
                             grown_year: o.czxbj.baseYear,
                             grown_rank: o.czxbj.data[0].pm, //成长排名
                             grown_stat_1: o.czxbj.data[0].jbmgsyzzlfh, //基本每股收益增长率(%)
                             grown_stat_2: o.czxbj.data[0].yysrzzlfh, //营业收入增长率(%)
                             grown_stat_3: o.czxbj.data[0].jlrzzlfh, //净利润增长率(%)
                             value_year: o.gzbj.baseYear,
                             value_rank: o.gzbj.data[0].pm, //估值排名
                             value_peg: o.gzbj.data[0].peg, //PEG
                             value_stat_1: o.gzbj.data[0].sylTTM, //市盈率
                             value_stat_2: o.gzbj.data[0].sslTTM, //市销率
                             value_stat_3: o.gzbj.data[0].sjlMRQ, //市净率
                             value_stat_4: o.gzbj.data[0].sxl1, //市现率①
                             value_stat_5: o.gzbj.data[0].sxl2, //市现率②
                             value_stat_6: o.gzbj.data[0].ev, //EV/EBITDA
                             dba_from_year: o.dbfxbj.startYear,
                             dba_rank: o.dbfxbj.data[0].pm, //杜邦分析排名
                             dba_stat_1: o.dbfxbj.data[0].roepj, //ROE(%) 三年平均
                             dba_stat_2: o.dbfxbj.data[0].jllpj, //净利率(%) 三年平均
                             dba_stat_3: o.dbfxbj.data[0].zzczzlpj, //总资产周转率(%) 三年平均
                             dba_stat_4: o.dbfxbj.data[0].qycspj, //权益乘数(%) 三年平均
                             scale_mv_rank: o.gsgmzsz[0].pm, // 总市值排名
                             scale_mv: o.gsgmzsz[0].zsz, //总市值(元)
                             scale_floating_rank: o.gsgmltsz[0].pm, // 流通市值排名
                             scale_floating: o.gsgmzsz[0].ltsz, //流通市值(元)
                             scale_income_rank: o.gsgmzsz[0].pm, // 营业收入排名
                             scale_income: o.gsgmyysr[0].yysr, //营业收入(元)
                             scale_nr_rank: o.gsgmjlr[0].pm, // 净利润排名
                             scale_nr: o.gsgmzsz[0].jlr, //净利润(元)
                             market: market,
                             code: stock.code,
                             name: stock.name
                         }
                     });
                 });
             });
         });
     });